﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5.Grades
{
    public partial class AddGrade : Form
    {
        private static Student s;
        private bool isBind = false;

        public AddGrade()
        {
            InitializeComponent();
            
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {

            
        }

        private void AddGrade_Load(object sender, EventArgs e)
        {
            
        }


        private void iniData()
        {
         
        }


        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(new DataGridViewRow());
            iniData();
        }

       

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //MessageBox.Show("进来了");
            //string cnoSelect = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value);
            //textBox1.Text = cnoSelect;
            
        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            string cnoSelect = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value);
            textBox1.Text = cnoSelect;

            //if (dataGridView1.CurrentCell.OwningColumn.HeaderText == "班级号")
            //{
            //    ((ComboBox)e.Control).SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);
            //    ((ComboBox)e.Control).SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
            //}
            if (dataGridView1.CurrentCell.OwningColumn.HeaderText == "班级号" && dataGridView1.CurrentCell.RowIndex != -1)
            {
                System.Windows.Forms.ComboBox cb = (System.Windows.Forms.ComboBox)e.Control;


                if (!isBind)
                {
                    cb.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
                    isBind = true;
                }
            }
            else
            {
                isBind = false;
            }
            //ComboBox cb = e.Control as ComboBox;
            //if (cb != null)
            //{
            //    // first remove event handler to keep from attaching multiple:
            //    cb.SelectedIndexChanged -= new
            //    EventHandler(cb_SelectedIndexChanged);

            //    // now attach the event handler
            //    cb.SelectedIndexChanged += new
            //    EventHandler(cb_SelectedIndexChanged);
            //}
        }


        /// <summary>  
        /// 清除事件绑定的函数  
        /// </summary>  
        /// <param name="objectHasEvents">拥有事件的实例</param>  
        /// <param name="eventName">事件名称</param>  
        /*public void ClearAllEvents(object objectHasEvents, string eventName)
        {
            if (objectHasEvents == null)
            {
                
                return;
            }
            try
            {
                EventInfo[] events = objectHasEvents.GetType().GetEvents(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (events == null || events.Length < 1)
                {
                    return;
                }
                for (int i = 0; i < events.Length; i++)
                {
                    EventInfo ei = events[i];
                    if (ei.Name == eventName)
                    {
                        FieldInfo fi = ei.DeclaringType.GetField(eventName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        if (fi != null)
                        {
                            fi.SetValue(objectHasEvents, null);
                        }
                        break;
                    }
                }
            }
            catch
            {
            }
        }*/

        private void ClearEvent()

        {

            PropertyInfo propertyInfo =
            (typeof(System.Windows.Forms.DataGridView)).
            GetProperty("Events", BindingFlags.Instance |



            BindingFlags.NonPublic);

            EventHandlerList eventHandlerList =
            (EventHandlerList)propertyInfo.GetValue(dataGridView1, null);

            FieldInfo fieldInfo = (typeof(DataGridView)).GetField("EVENT_" + "DATAGRIDVIEW" + "EDITINGCONTROLSHOWING", BindingFlags.Static |



            BindingFlags.NonPublic);

            Delegate d = eventHandlerList[fieldInfo.GetValue(this.dataGridView1)];

            if (d != null)

            {
                
                foreach (Delegate temp in d.GetInvocationList())

                {

                    //这个地方可以清除所有的委托,也可以使用条件清除指定委托,没有办法直接清除所有的

                }

            }

        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            isBind = false;

        }
    }


}

